package com.farm.controller.admin;

import com.farm.dto.GiftDTO;
import com.farm.dto.GiftPageQueryDTO;
import com.farm.entity.Gift;
import com.farm.result.PageResult;
import com.farm.result.Result;
import com.farm.service.GiftService;
import com.farm.vo.GiftVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @FileName GiftController
 * @Description
 * @Author Sword
 * @date 2025-02-25
 **/

@RestController
@RequestMapping("/admin/gift")
@Slf4j
@Api(tags = "礼盒相关接口")
public class GiftController {

    @Autowired
    private GiftService giftService;

    /**
     * 新增礼盒
     *
     * @param giftDTO
     * @return
     */
    @PostMapping
    @ApiOperation("新增礼盒")
    @CacheEvict(cacheNames = "giftCache",key = "#giftDTO.categoryId")
    public Result save(@RequestBody GiftDTO giftDTO) {
        log.info("新增礼盒:{}", giftDTO);
        giftService.save(giftDTO);
        return Result.success();
    }

    /**
     * 分页查询
     *
     * @param giftPageQueryDTO
     * @return
     */
    @GetMapping("/page")
    @ApiOperation("分页查询")
    public Result<PageResult> page(GiftPageQueryDTO giftPageQueryDTO) {
        log.info("分页查询:{}", giftPageQueryDTO);
        PageResult pageResult = giftService.page(giftPageQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * 礼盒起售、停售
     *
     * @param status
     * @param id
     * @return
     */
    @PostMapping("/status/{status}")
    @ApiOperation("礼盒起售、停售")
    @CacheEvict(cacheNames = "giftCache",allEntries = true)
    public Result startOrStop(@PathVariable Integer status, Long id) {
        log.info("礼盒起售、停售:{},{}", status, id);
        giftService.startOrStop(status, id);
        return Result.success();
    }


    /**
     * 根据id查询礼盒
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation("根据id查询礼盒")
    public Result<GiftVO> getById(@PathVariable Long id) {
        log.info("根据id查询礼盒:{}", id);

        GiftVO giftVO = giftService.getById(id);

        return Result.success(giftVO);
    }


    /**
     * 修改礼盒
     *
     * @param giftDTO
     * @return
     */
    @PutMapping
    @ApiOperation("修改礼盒")
    @CacheEvict(cacheNames = "giftCache",allEntries = true)
    public Result update(@RequestBody GiftDTO giftDTO) {
        log.info("修改礼盒:{}", giftDTO);
        giftService.update(giftDTO);
        return Result.success();
    }

    /**
     * 批量删除礼盒
     *
     * @param ids
     * @return
     */
    @DeleteMapping
    @ApiOperation("批量删除礼盒")
    @CacheEvict(cacheNames = "giftCache",allEntries = true)
    public Result delete(@RequestParam List<Long> ids) {
        log.info("批量删除礼盒:{}", ids);
        giftService.deleteByIds(ids);
        return Result.success();
    }
}
